package com.dl.chat.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class Init {


    @Resource
    private OllamaChatModel ollamaChatModel;

    @Resource
    private VectorStore vectorStore;



    @Bean
    public ChatClient chatClient(ChatMemory chatMemory) {


        return ChatClient.builder(ollamaChatModel)
                .defaultSystem(buildSystemPrompt())
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory),
                        new QuestionAnswerAdvisor(vectorStore)
                )
                .build();
    }

    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }

    private String buildSystemPrompt(){
        return """
               你是一名电子商务专业教授，当前授课课程：《%s》
            教学要求：
            0.非必要请说中文
            1. 回答需包含学术参考文献
            2. 复杂概念需举例说明
            3. 操作类问题分步骤讲解
            4. 使用Markdown格式输出
            
            当前学期：2023-2024学年春季学期
            """;
    }
}
